<div ng-controller="Provider_ToProviderC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">入库单</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive" ng-show="isshow">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <!--<div class="form-group">-->
                                <!--<label>供应商</label>-->
                                <!--<select ng-change="change1()" required id="Provider" class="form-control cangku" ng-model="orderdata.Provider" name="cangku" ng-options="value.Name for value in cangku"></select>-->
                            <!--</div>-->
                            <div class="form-group">
                                <label>收货仓库</label>
                                <select required class="form-control cangku" ng-model="orderdata.Provider" ng-options="value.Name for value in cangku1" ng-change="removeStockGoods()"></select>
                            </div>
                            <div class="form-group">
                                <label>每吨运费</label>
                                <input required ng-model="orderdata.DeliveryPrice" type="number" min="0" class="form-control">
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label>联系人信息(非必填)</label>-->
                                <!--<input ng-model="orderdata.Contact" type="text" class="form-control" placeholder="请输入联系人信息（100字内）" maxlength="100">-->
                            <!--</div>-->
                            <div class="form-group">
                                <label>备注(非必填)</label>
                                <input ng-model="orderdata.Remark" type="text" class="form-control" placeholder="请输入备注信息（100字内）" maxlength="100">
                            </div>
                        </div>
                        <!--<div class="container-fluid text-center bg-light lter">-->
                            <!--<div class="row">-->
                                <!--<h3 style="display: inline-block; margin: 0 auto">采购商品列表</h3>-->
                            <!--</div>-->
                            <!--<div class="row text-right" style="margin-bottom: 10px">-->
                                <!--<button type="button" class="btn btn-sm btn-success" ng-click="xxx()">去选商品</button>-->
                            <!--</div>-->
                        <!--</div>-->
                        <div class="container-fluid bg-light lter">
                            <div class="row" style="height: 40px;">
                                <div class="col-xs-6 col-md-6 text-left">
                                    <strong style="display: inline-block;line-height: 40px">入库商品列表</strong>
                                </div>
                                <a>
                                    <div ng-click="xxx()" class="col-xs-6 col-md-6 text-right" style="display: inline-block;line-height: 100%;height: 100%">
                                    <span style="display: inline-block;line-height: 40px">去选商品&nbsp;&nbsp;
                                        <i class="fa fa-chevron-right text"></i>
                                    </span>
                                    </div>
                                </a>
                            </div>
                        </div>
                        <div class="container-fluid" style="padding-top: 10px">
                            <!--<div class="row" style="border-bottom: red solid 1px" ng-repeat="item in orderdata.StockGoods">-->
                                <!--<div class="col-xs-9 col-md-9">-->
                                    <!--<div>名称/材质/规格：{{item.Name}}/{{item.Standard}}/{{item.Format}}</div>-->
                                    <!--<div style="margin-bottom: 5px">单价    ：<input type="number" min="0" ng-model="item.Price">/t</div>-->
                                    <!--<div style="margin-bottom: 5px">数量    ：<input type="number" min="0" ng-model="item.Count"></div>-->
                                    <!--<div style="margin-bottom: 5px">总质量  ：<input type="number" min="0" ng-model="item.TotalWeight"></div>-->
                                    <!--<div style="margin-bottom: 5px">不含税价：<input type="number" min="0" ng-model="item.NormPrice"></div>-->
                                    <!--&lt;!&ndash;<div>总价：{{item.Count}}*{{item.Price}}*{{item.Weight}}t={{item.Count*item.Price*item.Weight.toFixed(2)}}</div>&ndash;&gt;-->
                                    <!--&lt;!&ndash;<div>总价：{{item.Count}}*{{item.Price}}*{{item.Weight}}t={{(item.Count*item.Price*item.Weight) | number:2}}</div>&ndash;&gt;-->
                                <!--</div>-->
                                <!--<div class="col-xs-3 col-md-3" style="text-align: center;">-->
                                    <!--<button type="button" class="btn btn-sm btn-default" ng-click="Countadd(item)" style="margin: 5px">-->
                                        <!--<i class="fa fa-plus text"></i>-->
                                    <!--</button>-->
                                    <!--<button type="button" class="btn btn-sm btn-default" ng-click="Countdeduct(item)" style="margin: 5px">-->
                                        <!--<i class="fa fa-minus text"></i>-->
                                    <!--</button>-->
                                    <!--<button type="button" class="btn btn-sm btn-danger" ng-click="deduct(item)" style="margin: 5px">-->
                                        <!--删除-->
                                    <!--</button>-->
                                <!--</div>-->
                            <!--</div>-->

                            <div class="panel panel-default" ng-repeat="item in orderdata.StockGoods" style="margin-bottom: 10px">
                                <div class="panel-heading">
                                    <div>品牌：{{item.Brand}}</div>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-xs-9 col-md-9">
                                            <div>名称/材质/规格：{{item.Name}}/{{item.Standard}}/{{item.Format}}</div>
                                            <div style="margin-bottom: 5px">单价    ：<input type="number" min="0" ng-model="item.Price">/t</div>
                                            <div style="margin-bottom: 5px">数量    ：<input type="number" ng-model="item.Count"></div>
                                            <div style="margin-bottom: 5px">总质量  ：<input type="number" ng-model="item.TotalWeight"></div>
                                            <div style="margin-bottom: 5px">不含税价：<input type="number" min="0" ng-model="item.NormPrice"><em ng-if="!item.NormPrice || item.NormPrice <=0" style="color:red">不得为空</em></div>
                                            <!--<div>总价：{{item.Count}}*{{item.Price}}*{{item.Weight}}t={{item.Count*item.Price*item.Weight.toFixed(2)}}</div>-->
                                            <!--<div>总价：{{item.Count}}*{{item.Price}}*{{item.Weight}}t={{(item.Count*item.Price*item.Weight) | number:2}}</div>-->
                                        </div>
                                        <div class="col-xs-3 col-md-3" style="text-align: center;">
                                            <button type="button" class="btn btn-sm btn-default" ng-click="Countadd(item)" style="margin: 5px">
                                                <i class="fa fa-plus text"></i>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-default" ng-click="Countdeduct(item)" style="margin: 5px">
                                                <i class="fa fa-minus text"></i>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-danger" ng-click="deduct(item)" style="margin: 5px">
                                                删除
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!--<div class="row text-left">-->
                                <!--<h4 style="display: inline-block">总价：{{orderdata.Total}}</h4>-->
                            <!--</div>-->
                            <!--<div class="row text-left">-->
                                <!--<h4 style="display: inline-block">预计运费价格：{{newWeight * orderdata.DeliveryPrice}}</h4>-->
                            <!--</div>-->
                            <!--<div class="row text-right">-->
                                <!--<button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="ordersub()">提交入库单</button>-->
                                <!--&lt;!&ndash;<button type="button" class="btn btn-default" ng-click="abolish()">返回列表页</button>&ndash;&gt;-->
                            <!--</div>-->
                            <div class="row text-center" style="height: 40px;">
                                <a>
                                    <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                        <button type="button" style="width: 100%;height: 100%" class="btn btn-success" ng-disabled="form.$invalid" ng-click="ordersub()">提交入库单</button>
                                    </div>
                                    <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                        <button type="button" style="width: 100%;height: 100%" class="btn btn-default" ng-click="abolish()">返回列表页</button>
                                    </div>
                                </a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div ng-show="!isshow">
                <div class="container-fluid">
                    <div class="row" style="height: 40px;">
                        <a>
                            <div ng-click="xxx()" class="col-xs-6 col-md-6 text-left" style="display: inline-block;line-height: 100%;height: 100%">
                                    <span style="display: inline-block;line-height: 40px">
                                        <i class="fa fa-chevron-left text"></i>&nbsp;&nbsp;返回订单
                                    </span>
                            </div>
                        </a>
                        <div class="col-xs-6 col-md-6 text-center">
                            <select style="" class="btn btn-default" ng-model="findName" ng-change="genre(findName)">
                                <option value="盘螺">盘螺</option>
                                <option value="线材">线材</option>
                                <option value="钢坯">钢坯</option>
                                <option value="螺纹钢">螺纹钢</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="container-fluid">
                    <div class="panel panel-default" ng-repeat="item in DepotCommodity" style="margin-bottom: 10px">
                        <div class="panel-heading">
                            <div>钢厂：{{item.Brand}}</div>
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-xs-9 col-md-9">
                                    <div>名称/材质/规格：{{item.Name}}/{{item.Standard}}/{{item.Format}}</div>
                                </div>
                                <div class="col-xs-3 col-md-3" style="text-align: center;">
                                    <button type="button" ng-show="!item.isadd" class="btn btn-sm btn-default" ng-click="add(item)">
                                        <i class="fa fa-plus text"></i>
                                        <span class="text">添加</span>
                                    </button>
                                    <button type="button" ng-show="item.isadd" class="btn btn-sm btn-default" ng-click="deduct(item)">
                                        <i class="fa fa-minus text"></i>
                                        <span class="text">取消</span>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
            //创建采购单控制器
            .controller("Provider_ToProviderC", function ($scope, $state, $http) {
                $scope.findName = '盘螺';
                $scope.isshow = true;
                $scope.isadd = true;
                $scope.Weight = 0;
                $scope.newWeight = 0;
//                获取列表方法
//                $scope.acquire = function(type){
//                    $http({
//                        url: "../api/Provider?type="+type+"&page=1&rows=9999",
//                        method: "GET"
//                    })
//                            .then(function (res) {
//                                if (res.State <= 0) {
//                                    alert(res.Msg);
//                                } else {
//                                    $scope.cangku = res.data.Data;
//                                }
//                            })
//                            .then(function (error) {
//                                if (error) {
//                                    console.log("error");
//                                }
//                            });
//                };
//                删除方法
                $scope.removeStockGoods = function(){
                    $scope.orderdata.StockGoods = []
                };
                //获取仓库列表方法
                $http({
                    url: "../api/Provider?type=1&page=1&rows=9999",
                    method: "GET"
                })
                        .then(function (res) {
                            if (res.State <= 0) {
                                alert(res.Msg);
                            } else {
                                $scope.cangku1 = res.data.Data;
                            }
                        })
                        .then(function (error) {
                            if (error) {
                                console.log("error");
                            }
                        });
                    //获取钢厂列表
//                    $scope.acquire(0);

                //提交编辑结果
                $scope.xxx = function () {
//                    if ($scope.orderdata.Provider) {
//                        $scope.sum();
//                        $scope.genre('盘螺');
//                        $scope.isshow = !$scope.isshow;
//                    } else {
//                        alert("请选择供应商")
//                    }
                    $scope.isshow = !$scope.isshow;
//                    $scope.sum();
                    $scope.genre('盘螺');
                };
//                $scope.change1 = function(){
//                    $http({
//                        url: "../api/Provider?type=1&page=1&rows=9999",
//                        method: "GET"
//                    })
//                            .then(function (res) {
//                                if (res.State <= 0) {
//                                    alert(res.Msg);
//                                } else {
//                                    $scope.cangku1 = res.data.Data;
//                                    if($scope.orderdata.Provider){
//                                        $scope.cangku1.unshift($scope.orderdata.Provider);
//                                    }
//                                }
//                            })
//                            .then(function (error) {
//                                if (error) {
//                                    console.log("error");
//                                }
//                            });
//                };
                //计算总价方法
//                $scope.sum = function () {
//                    $scope.newWeight = 0;
//                    $scope.orderdata.Total = 0;
//                    $scope.Weight = 0;
//                    for (var i = 0; i < $scope.orderdata.StockGoods.length; i++) {
//                        $scope.Weight = $scope.orderdata.StockGoods[i].Count * $scope.orderdata.StockGoods[i].Weight;
//                        $scope.orderdata.Total += $scope.Weight * $scope.orderdata.StockGoods[i].Price;
//
//                        $scope.newWeight += $scope.orderdata.StockGoods[i].Count*$scope.orderdata.StockGoods[i].Weight;
//                    }
//                    $scope.orderdata.SourceTotal = $scope.orderdata.Total;
//                    $scope.orderdata.Total = $scope.orderdata.Total.toFixed(2);
//                    $scope.orderdata.DeliveryPriceTotal = $scope.newWeight*$scope.orderdata.DeliveryPrice;
//                };

                $scope.genre = function (name) {
//                    if (name != "" && $scope.orderdata.Provider.Supplier.Id != "") {
//                        if ($scope.type == 0) {
//
//                            $scope.model1 = $scope.orderdata.Provider.Supplier.Id;
//                        }else if($scope.type == 1){
//                            $scope.model1 = "";
//                        }
                        $scope.model1 = "";
                        //获取商品列表
                        $http({
                            url: "../api/Supplier/Commodity?supplierId=" + $scope.model1 + "&name=" + name + "&format=&page=1&rows=9999",
                            method: "GET"
                        })
                                .then(function (res) {
                                    if (res.State <= 0) {
                                        alert(res.Msg);
                                    } else {
                                        $scope.DepotCommodity = res.data.Data;
                                        console.log($scope.DepotCommodity);
                                    }
                                })
                                .then(function (error) {
                                    if (error) {
                                        console.log("error");
                                    }
                                });
//                    }
                };
                //添加到购物车
                $scope.add = function (item) {
                    for (var i = 0; i < $scope.orderdata.StockGoods.length; i++) {
                        if ($scope.orderdata.StockGoods[i].Id == item.Id) {
                            alert("您已添加过此商品，不能重复添加！");
                            return;
                        }
                    }
                    item.isadd = true;
                    item.Count = 1;
                    item.Price = 1;
                    item.TotalWeight = 1;
                    item.NormPrice = 1;
//                    $scope.sum();
                    $scope.orderdata.StockGoods.push(item);
                };
                //供货商改变清空购物车
//                $("#Provider").change(function () {
//                    $scope.orderdata.StockGoods = [];
//                    $scope.sum();
//                });
                //从购物车删除
                $scope.deduct = function (item) {
                    item.isadd = false;
                    item.Count = 0;
//                    $scope.sum();
                    for (var i = 0; i < $scope.orderdata.StockGoods.length; i++) {
                        if ($scope.orderdata.StockGoods[i].Id == item.Id) {
                            $scope.orderdata.StockGoods.splice(i, 1);
                        }
                    }
                };
                //购物车增加数量
                $scope.Countadd = function (item) {
                    item.Count += 1;
//                    $scope.sum();
                };
                //购物车减少数量
                $scope.Countdeduct = function (item) {
                    if (item.Count > 0) {
                        item.Count -= 1;
//                        $scope.sum();
                    }
                };
                $scope.orderdata = {
                    StockGoods: [],
                    DeliveryPrice : 0
                };
                //提交采购单
                $scope.ordersub = function () {
//                    $scope.orderdata.TotalWeight = $scope.newWeight;
//                    $scope.orderdata.DeliveryPriceTotal = $scope.newWeight*$scope.orderdata.DeliveryPrice;
                    $scope.orderdata.ProviderId = $scope.orderdata.Provider.Id;
                    if ($scope.orderdata.StockGoods.length < 1) {
                        alert("请添加商品")
                    } else {
                        for (var i = 0; i < $scope.orderdata.StockGoods.length; i++) {
                            $scope.orderdata.StockGoods[i].Commodity = {Id:$scope.orderdata.StockGoods[i].Id};
//                            delete $scope.orderdata.StockGoods[i].Id;
                        }
//                        console.log($scope.orderdata);
                        $http.post('../api/Provider/CreateAndRunEntry', $scope.orderdata)
                                .success(function (res) {
                                    if (res.State <= 0) {
                                        alert(res.Msg);
                                    } else {
                                        alert("添加成功");
                                        $state.go('app.Provider_DepotCommodityList' , {id:$scope.orderdata.ProviderId,Name:$scope.orderdata.Provider.Name,type:1});
                                    }
                                })
                                .error(function(){
                                    console.log($scope.orderdata);
                                })
                        ;
                    }
                };
//                返回上一页
                $scope.abolish = function () {
                    $state.go('app.Provider_ToProviderList');
                };
            })
</script>